package com.rm.zeyou.util.barCode;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.rm.zeyou.util.uploadUtil.FileParseMultipartFile;
import com.rm.zeyou.util.uploadUtil.JSCHUploadFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BarCodeUtils {
    /**
     * generateCode 根据code生成相应的一维码
     *
     * @param file   一维码目标文件
     * @param code   一维码内容
     * @param width  图片宽度
     * @param height 图片高度
     */
    public static Map<String, String> generateCode(File file, String code, int width, int height) {
        //定义位图矩阵BitMatrix
        BitMatrix matrix = null;
        try {
            // 使用code_128格式进行编码生成100*25的条形码
            MultiFormatWriter writer = new MultiFormatWriter();

            matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null);
            //matrix = writer.encode(code,BarcodeFormat.EAN_13, width, height, null);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        FileOutputStream outStream = null;
        //将位图矩阵BitMatrix保存为图片
        try {
            outStream = new FileOutputStream(file);
            ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png",
                    outStream);
            outStream.flush();
            Map<String, String> map = new HashMap<>();
            MultipartFile multipartFile = FileParseMultipartFile.getMultipartFile(file);
            String s = JSCHUploadFile.uploadFile(multipartFile);
            System.out.println(s);
            String s1 = readCode(file);
            map.put("path", s);
            map.put("text", s1);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            file.delete();
        }
        return null;
    }

    /**
     * readCode 读取一张一维码图片
     *
     * @param file 一维码图片名字
     */
    public static String readCode(File file) {
        String response = null;
        try {
            BufferedImage image = ImageIO.read(file);
            if (image == null) {
                return null;
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            Map<DecodeHintType, Object> hints = new HashMap<>();
            hints.put(DecodeHintType.CHARACTER_SET, "GBK");
            hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
            hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

            Result result = new MultiFormatReader().decode(bitmap, hints);
            System.out.println("条形码内容: " + result.getText());
            response = result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static void main(String[] args) throws Exception {
        Map<String, String> map = generateCode(new File("1dcode.png"), "123456789012", 500, 250);
//        log.info("map：{}", JSON.toJSONString(map,true));
//        readCode(new File("1dcode.png"));
    }
}
